{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">

            <div class="layui-form">
                <script type="text/html" id="options">
                    <div class="layui-btn-group">
                        <a class="layui-btn layui-btn-sm {:auth('business.order/get_edit')}" lay-event="edit">订单详情</a>
                    </div>
                </script>

                <div class="layui-input-inline">
                    <input type="text" name="order_sn" id="order_sn" placeholder="请输入订单号" class="layui-input">
                </div>
                <button class="layui-btn" id="searchBtn"><i class="layui-icon">&#xe615;</i>搜 索</button>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>

            <script type="text/html" id="thumb">
                {{#  if(d.goods.thumb){ }}
                <a href="{{d.goods.thumb}}" target="_blank" title="点击查看">
                    <img src="{{d.goods.thumb}}" alt="" width="28" height="28">
                </a>
                {{#  } }}
            </script>
            <script type="text/html" id="user">
                {{#  if(d.User){ }}
                {{d.User.nick_name}}
                {{#  } else { }}
                用户不存在
                {{#  } }}
            </script>
            <script type="text/html" id="category">
                {{#  if(d.Goods){ }}
                {{d.Goods.title}}
                {{#  } else { }}
                商品不存在
                {{#  } }}
            </script>
            <script type="text/html" id="store">
                {{d.store.name}}
            </script>
        </div>
    </div>
</div>
<!-- 表单弹窗 -->


<!-- js部分 -->
{include file='public/footer' /}
<!--<script type="text/javascript" src="/static/public/assets/js/common.js?v=315"></script>-->
<script>
    layui.use(['layer', 'table', 'admin','form'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var table = layui.table;
        //用户表格初始化
        var dataTable =
                table.render({
                    elem: '#dataTable'
                    , autoSort: false
                    , height: 500
                    , url: "{:__url('business.order/getData')}" //数据接口
                    , page: true //开启分页
                    , cols: [[ //表头
                        {checkbox: true, fixed: true}
                        /* , {field: 'id', title: 'ID', sort: true, width: 60}*/
                        , {field: 'order_sn', title: '订单编号', sort: true, width: 180}
                        , {field: 'category', title: '商品名称', toolbar: '#category'}
                        , {field: 'user', title: '用户呢称', toolbar: '#user'}
                        , {field: 'amount', title: '耗费积分', sort: true, width: 150}

                        /*, {field: 'thumb', title: '缩略图', align: 'center',toolbar: '#thumb', width: 100}*/

                        ,{
                            title: '状态', templet: function (d) {
                                var strs = ['<span class="layui-badge layui-bg-blue">待成团</span>',
                                    '<span class="layui-badge layui-bg-green">已成团,待领取</span>',
                                    '<span class="layui-badge layui-bg-green">退款中</span>',
                                    '<span class="layui-badge layui-bg-red" >已退款</span>',
                                    '<span class="layui-badge layui-bg-orange">已评价</span>',
                                    '<span class="layui-badge layui-bg-orange">已领取</span>',
                                    '<span class="layui-badge layui-bg-red">拼团失败</span>',
                                ];
                                return strs[d.status];
                            }, align: 'center',width: 200
                        }
                        , {field: 'create_time', title: '下单时间'}

                        , {fixed: 'right', width: 140, align: 'center',title:'操作', toolbar: '#options'}
                    ]]
                });

        //监听工具条
        table.on('tool(dataTable)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                layer.confirm('确认删除吗？', function (index){
                    layer.close(index);
                    var load = layer.load();
                    $.post("{:__url('business.order/delete')}", {
                        _method: 'delete',
                        ids: [data.id]
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1}, function () {
                                obj.del();
                            })
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                showEditModel('查看订单'+data.order_sn,'/business.order/get_edit?id=' + data.id);

            }
        });


        // 显示表单弹窗
        function showEditModel(title,url) {
            admin.open({
                type: 2,
                offset:'r',
                title: title,
                content: url,
                btn:['确认','取消']
                , area: ['500px', '100%'],
                btnAlign: 'c',   // 按钮居中
                success: function (layero, dIndex) {
                    form.render();    // 表单渲染
                },

                yes: function (index, layero) {

                    // 获取弹出层中的form表单元素
                    var formSubmit=layer.getChildFrame('form', index);
                    //获取表单数据
                    var data = {dosubmit:1};
                    var action = formSubmit[0]['action'];
                    var a = formSubmit.serializeArray();
                    $.each(a, function () {
                        if (data[this.name] !== undefined) {
                            if (!data[this.name].push) {
                                data[this.name] = [data[this.name]];
                            }
                            data[this.name].push(this.value || '');
                        } else {
                            data[this.name] = this.value || '';
                        }
                    });

                    if (data.parent_id == '') {
                        data.parent_id = '0';
                    }

                    layer.load(2);
                    $.post(action, data, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);

                            dataTable.reload({

                                page: {curr: 1}
                            })

                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');

                    return false;

                },
                btn2: function (index, layero) {
                    layer.close(index);
                }


            });
        }



        //搜索
        $("#searchBtn").click(function () {
            var catId = $("#category_id").val();
            var order_sn = $("#order_sn").val();
            dataTable.reload({
                where: {category_id: catId, order_sn: order_sn},
                page: {curr: 1}
            })
        })
    })
</script>

</body>
</html>
